package net.lab1024.smartadmin.module.business.front.controller;

import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.front.domain.dto.FrontAddDTO;
import net.lab1024.smartadmin.module.business.front.domain.dto.FrontQueryDTO;
import net.lab1024.smartadmin.module.business.front.domain.dto.FrontUpdateDTO;
import net.lab1024.smartadmin.module.business.front.domain.vo.FrontExcelVO;
import net.lab1024.smartadmin.module.business.front.domain.vo.FrontVO;
import net.lab1024.smartadmin.module.business.front.service.FrontService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *peony
 * @version 1.0
 * @since JDK1.8
 */
@RestController
@Api(tags = {""})
public class FrontController extends BaseController {

    @Autowired
    private FrontService frontService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/front/page/query")
    public ResponseDTO<PageResultDTO<FrontVO>> queryByPage(@RequestBody FrontQueryDTO queryDTO) {
        return frontService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "添加")
    @PostMapping("/front/add")
    public ResponseDTO<String> add(@RequestBody @Validated FrontAddDTO addTO){
        return frontService.add(addTO);
    }

    @ApiOperation(value="修改")
    @PostMapping("/front/update")
    public ResponseDTO<String> update(@RequestBody @Validated FrontUpdateDTO updateDTO){
        return frontService.update(updateDTO);
    }

    @ApiOperation(value="批量删除")
    @PostMapping("/front/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return frontService.deleteByIds(idList);
    }

    @ApiOperation(value = "批量导出")
    @PostMapping("/front/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<FrontExcelVO> frontList = frontService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("美容美发类别", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, FrontExcelVO.class, frontList);
        downloadExcel("美容美发类别", workbook, response);
    }

    @ApiOperation(value = "导出全部")
    @PostMapping("/front/export/all")
    public void exportAll(@RequestBody @Validated FrontQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<FrontExcelVO> frontList = frontService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("美容类别", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, FrontExcelVO.class, frontList);
        downloadExcel("美容类别", workbook, response);

    }

}
